import pygame


class MyControl(object):
    def __init__(self, rect, img_file, img_cx, text, font_info):
        self.status = 0
        self.rect = rect
        self.img_cx = img_cx
        self.text = text
        self.font_info = font_info

        if img_file is None:
            self.__img = pygame.image.load(img_file)
            self.__image = []

            img_rect = self.__img.get_rect()
            width = int(img_rect.width / img_cx)

            x = 0
            for i in range(self.img_cx):
                self.__img.append(self.__img.subsurface((x, 0), (width, img_rect.height)))
                x += width

            self.img__width = width

        if text == "":
            self.label = None
        else:
            self.label = Label(rect.left, rect.top, text, font_info)
    def render(self, surface):
        if self.status >= 0:
            if self.__img is not None:
                if self.status < self.img_cx:
                    surface.blit(self.__image[self.status], (self.rect.left, self.rect.top))

            elif self.label is not None:
                self.label.render(surface)

    def is_over(self, point):
        if self.status <= 0:
            bflag = False
        else:
            bflag = self.rect.collodepoint(point)

        return bflag

    def check_click(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            return self.is_over(event.pos)

    def hide(self):
        self.status = -1

    def disabled(self):
        self.status = 0

    def enabled(self):
        self.status = 1
